
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
//公元1年1月1日是星期一，星期=总天数模7，0(星期日)---6(星期六) 
int check_year(int year){//run
	
	if((year%400==0)||(year%4==0&&year%100!=0)){
		return 1;
	}else{
		return 0;
	}
}

int week_f(int year){//返回xx年1月1日是星期几

	if(year==1){
		return 1;
	}
 
	int sum=0;
	for(int i=1;i<year;i++){
		if(check_year(i)==1){
			sum=sum+366;
		} else{
			sum=sum+365;
		}
	}
	return (sum+1)%7;
}


int month_run(int n){
	switch(n){
		case 1:return 31;
		case 2:return 29;
		case 3:return 31;
		case 4:return 30;
		case 5:return 31;
		case 6:return 30;
		case 7:return 31;
		case 8:return 31;
		case 9:return 30;
		case 10:return 31;
		case 11:return 30;
		case 12:return 31;
	}
}

int month_ping(int n){
	switch(n){
		case 1:return 31;
		case 2:return 28;
		case 3:return 31;
		case 4:return 30;
		case 5:return 31;
		case 6:return 30;
		case 7:return 31;
		case 8:return 31;
		case 9:return 30;
		case 10:return 31;
		case 11:return 30;
		case 12:return 31;
	}
}

void run(int week,int x){
	int enter=0;
	int wee=week;
	for(int i=1;i<=12;i++){
	if (i!=x)
	{
		continue;
	}
		printf("_______________________%d_________________________\n",i);
		printf("S\tM\tT\tW\tT\tF\tS\n") ;
		
		
		for(int k=0;k<wee;k++){
			printf("	");
			enter++;
		} 
		
		
		for(int j=1;j<=month_run(i);j++){
			if(enter%7==0){
				
				printf("\n");
			}
			printf("%d	",j);
			enter++;	
		}
		
		wee=(wee+month_run(i))%7;
		enter=enter-wee;
		
		printf("\n"); 
		
	}
}



 void ping(int week,int x){
	int enter=0;
	int wee=week;
	for(int i=1;i<=12;i++){//12
	if (i!=x)
	{
		continue;
	}
	
		printf("_______________________%d_________________________\n",i);
		printf("S\tM\tT\tW\tT\tF\tS\n") ;
		for(int k=0;k<wee;k++){
			printf("	");
			enter++;
		} 
		for(int j=1;j<=month_ping(i);j++){
			if(enter%7==0){
				printf("\n");
			}
			printf("%d	",j);
			enter++;	
		}
		wee=(wee+month_ping(i))%7;
		enter=enter-wee;
		
		printf("\n"); 
	}
}
int main(){
	int year;
	int week=0;
	int sum=0; 
	int x=0;
	time_t curtime;
   	time(&curtime);
	printf("now : %s", ctime(&curtime));
	printf("enter a year/month or enter 0/0:");
	scanf("%d/%d",&year,&x); 
	week=week_f(year);
    if ((year==2023&& x==10)||year==0)
	{
	printf("_______________________10_________________________\nS       M       T       W       T       F       S\n1       2       3       4       5       6       7\n8       9       10      11      12      13      14\n15      16      17      18      19      20      21\n22      23      \033[4;31;42m24\033[0m      25      26      27      28\n29      30      31");
	return 0;
	}
	

	if(check_year(year)==1){
		run(week,x);
		printf("run\n");
	} else{
		ping(week,x);
		printf("ping\n");
	}
    system("PAUSE");
	return 0;
}